<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2018/9/2
 * Time: 下午9:32
 */

namespace api\controllers;

use yii\rest\ActiveController;
use yii\web\Response;
use yii\helpers\ArrayHelper;
use common\helpers\HttpHeaderAuth;

class ARBaseController extends ActiveController
{


    public $enableCsrfValidation = false;

    public function behaviors()
    {
        $behaviors                                              = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;//默认浏览器打开返回json
        unset($behaviors['authenticator']);

        //设置跨域
        $behaviors['corsFilter'] = [
            'class' => \yii\filters\Cors::className(),
            'cors'  => [
                'Origin'                           => static::allowedDomains(),
                'Access-Control-Request-Method'    => ['POST', 'PUT', 'GET', 'OPTIONS', 'DELETE'],
                'Access-Control-Allow-Credentials' => true,
                'Access-Control-Max-Age'           => 86400,
                'Access-Control-Request-Headers'   => ['*'],
            ],
        ];

        $behaviors['authenticator'] = [
            'class'  => HttpHeaderAuth::className(),
            'except' => ['OPTIONS'],
        ];

        return $behaviors;
    }

    public static function allowedDomains()
    {
        return ['*',];
    }
}
